package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.asset.model.entity.AssetMaster;
import com.erpanderp.wam.asset.model.entity.SystemAssets;
import com.erpanderp.wam.asset.model.service.AssetMasterDAO;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateNewAssetBean implements Serializable{

	private static final long serialVersionUID = 1L;

	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private Map<String,String> assetStatusMap = CommonLookup.getAssetStatus();
	private Map<String,String> businessImpactMap = CommonLookup.getAssetBusinessImpact();
	private Map<String,String> assetCategoryMap = CommonLookup.getAssetCategory();
	private TimeZone timeZone = TimeZone.getDefault();
	
	private SystemAssets systemAssets=new SystemAssets();
	private AssetMaster assetMaster=new AssetMaster();
	private AssetMaster editAssetMaster=new AssetMaster();
	private CustomLookup customLookUp = new CustomLookup();
	
	private List<LookupValue> systemAssetsList=new ArrayList<LookupValue>();
	private Map<String, String> assetsTypeMap = new HashMap<String, String>();
	private List<AssetMaster> assetMasterEditList=new ArrayList<AssetMaster>();
	private List<AssetMaster> allAssetsList=new ArrayList<AssetMaster>();
	
	private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	private AssetMasterDAO assetMasterDAO=new AssetMasterDAO();
	private CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	
	private String selectedAssetStatus;
	private String selectedAssetType;
	private String selectedViewAssetType;
	private String selectedViewAssetStatus;
	private boolean FORM_VALID;
	private boolean EDIT_VALID;
	private boolean EDIT_PARAMS_VALID;
	private boolean VIEW_VALID;
	private boolean assetEditListRender;
	private int editSno=1;
	private int viewSno=1;

	public CreateNewAssetBean() {
		
		customLookUp = customLookupDAO.getCustomLookUp(CommonLookup.ASSETTYPE_CUSTOM_LOOKUP);
		systemAssetsList = customLookupDAO.getLookupValuesList(customLookUp.getCustomLookId());
		for(LookupValue lookUpValue : systemAssetsList){
			assetsTypeMap.put(lookUpValue.getLookupName(), lookUpValue.getLookupValue());
		}
		/*systemAssetsList = assetRequestDAO.getAllAssets();
		for (SystemAssets systemAssets : systemAssetsList) {
			assetsTypeMap.put(systemAssets.getAssetName(), systemAssets.getAssetName());
		}*/
	}
	
	public void confirmationAction(ActionEvent e){
		validateForm();	
		if(FORM_VALID){	
			RequestContext.getCurrentInstance().execute("confirmation.show();");
		}else {
			RequestContext.getCurrentInstance().execute("confirmation.hide();");
		}
    }
	
	public void EditConfirmationAction(ActionEvent e){
		editForm();
		if(EDIT_PARAMS_VALID){	
			RequestContext.getCurrentInstance().execute("confirmationEdit.show();");
		}else {
			RequestContext.getCurrentInstance().execute("assetEditDialog.show();");
		}
    }
	
	public void assetEditActionListener(ActionEvent e){
		editValidateForm();	
		if(EDIT_VALID){			
			assetMasterEditList = assetMasterDAO.getAllAssetsByAssetStatus(getSelectedAssetType(),getSelectedAssetStatus());
			editSno=1;
			for(AssetMaster assetMaster:assetMasterEditList){
				assetMaster.setSno(editSno);
				editSno++;
			}
			assetEditListRender=true;
		}
    }
	public void assetViewValidation(ActionEvent e){
		viewValidateForm();	
		if(VIEW_VALID){	
			
			if(selectedViewAssetStatus.equalsIgnoreCase("All")){
				allAssetsList = assetMasterDAO.getAllAssets(selectedViewAssetType);
			}else{
				allAssetsList = assetMasterDAO.getAllAssetsByAssetStatus(getSelectedViewAssetType(),getSelectedViewAssetStatus());
			}	
			viewSno=1;
			for(AssetMaster viewAssets:allAssetsList){
				viewAssets.setSno(viewSno);
				viewSno++;
			}
			assetEditListRender=true;
		}
    }

	public String saveAssetRequestAction(){	
		validateForm();	
			if(FORM_VALID){				
				assetMaster.setEmployee(wamUser.getEmployee());				
				assetMaster.setCreateDate(new Date());
				assetMaster.setCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
				assetMaster.setStatus(CommonLookup.STATUS_ACTIVE);
				assetMaster.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
				assetMaster.setDateModified(new Date());				 
				assetMasterDAO.saveOrUpdateAssetMaster(assetMaster);				 
				return "createnewasset";
			}else{				
				return null;
			}		
		}
	
	public String saveEditAssetAction(){
		editForm();
		if(EDIT_PARAMS_VALID){				
			editAssetMaster.setEmployee(wamUser.getEmployee());			
			editAssetMaster.setModifiedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			editAssetMaster.setDateModified(new Date());			 
			assetMasterDAO.saveOrUpdateAssetMaster(editAssetMaster);			 
			return "createnewasset";
		}else{
			return null;
		}		
    }
	//Final validation 
	private void validateForm(){
		FORM_VALID=true;		
		if(ValidationUtil.isEmpty((assetMaster.getAcquisitionDate()!=null) ? assetMaster.getAcquisitionDate().toString():null, "Acquisition Date", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(assetMaster.getAssetDescription(), "Asset Description", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(assetMaster.getAssetCode(), "Asset Code", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}else{
		 boolean isAssetCodeExists;
		 isAssetCodeExists=assetMasterDAO.isAssetCodeExists(assetMaster.getAssetCode());
		 System.out.println("============isProfileExists=====After======"+isAssetCodeExists);
			if(isAssetCodeExists){
				FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "This asset code is already exists.", ""));
				FORM_VALID = false;
			}
		}
	}
	private void editForm(){
		EDIT_PARAMS_VALID=true;		
		
		if(ValidationUtil.isEmpty((editAssetMaster.getAcquisitionDate()!=null) ? editAssetMaster.getAcquisitionDate().toString():null, "Acquisition Date", null,
				FacesContext.getCurrentInstance()) != null) {
			EDIT_PARAMS_VALID = false;
		}
		if(ValidationUtil.isEmpty(editAssetMaster.getAssetDescription(), "Asset Description", null, FacesContext.getCurrentInstance()) != null){
			EDIT_PARAMS_VALID = false;
		}
		if(ValidationUtil.isEmpty(editAssetMaster.getAssetCode(), "Asset Code", null, FacesContext.getCurrentInstance()) != null){
			EDIT_PARAMS_VALID = false;
		}
	
	}
	private void editValidateForm(){
		EDIT_VALID=true;		

		if(ValidationUtil.isEmpty(getSelectedAssetStatus(), "Asset Status", null, FacesContext.getCurrentInstance()) != null){
			EDIT_VALID = false;
		}
		if(ValidationUtil.isEmpty(getSelectedAssetType(), "Asset Type", null, FacesContext.getCurrentInstance()) != null){
			EDIT_VALID = false;
		}
	}
	private void viewValidateForm(){
		VIEW_VALID=true;		

		if(ValidationUtil.isEmpty(selectedViewAssetStatus, "Asset Status", null, FacesContext.getCurrentInstance()) != null){
			VIEW_VALID = false;
		}
		if(ValidationUtil.isEmpty(selectedViewAssetType, "Asset Type", null, FacesContext.getCurrentInstance()) != null){
			VIEW_VALID = false;
		}
	}
		

	//SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public SystemAssets getSystemAssets() {
		return systemAssets;
	}

	public void setSystemAssets(SystemAssets systemAssets) {
		this.systemAssets = systemAssets;
	}

	

	public Map<String, String> getAssetsTypeMap() {
		return assetsTypeMap;
	}

	public void setAssetsTypeMap(Map<String, String> assetsTypeMap) {
		this.assetsTypeMap = assetsTypeMap;
	}

	public AssetMaster getAssetMaster() {
		return assetMaster;
	}

	public void setAssetMaster(AssetMaster assetMaster) {
		this.assetMaster = assetMaster;
	}
	public AssetMasterDAO getAssetMasterDAO() {
		return assetMasterDAO;
	}
	public void setAssetMasterDAO(AssetMasterDAO assetMasterDAO) {
		this.assetMasterDAO = assetMasterDAO;
	}
	public String getSelectedAssetStatus() {
		return selectedAssetStatus;
	}
	public void setSelectedAssetStatus(String selectedAssetStatus) {
		this.selectedAssetStatus = selectedAssetStatus;
	}
	public boolean isEDIT_VALID() {
		return EDIT_VALID;
	}
	public void setEDIT_VALID(boolean eDIT_VALID) {
		EDIT_VALID = eDIT_VALID;
	}
	public String getSelectedAssetType() {
		return selectedAssetType;
	}
	public void setSelectedAssetType(String selectedAssetType) {
		this.selectedAssetType = selectedAssetType;
	}
	public boolean isAssetEditListRender() {
		return assetEditListRender;
	}
	public void setAssetEditListRender(boolean assetEditListRender) {
		this.assetEditListRender = assetEditListRender;
	}
	public List<AssetMaster> getAssetMasterEditList() {
		return assetMasterEditList;
	}
	public void setAssetMasterEditList(List<AssetMaster> assetMasterEditList) {
		this.assetMasterEditList = assetMasterEditList;
	}
	public int getEditSno() {
		return editSno;
	}
	public void setEditSno(int editSno) {
		this.editSno = editSno;
	}
	public AssetMaster getEditAssetMaster() {
		return editAssetMaster;
	}
	public void setEditAssetMaster(AssetMaster editAssetMaster) {
		this.editAssetMaster = editAssetMaster;
	}
	public boolean isEDIT_PARAMS_VALID() {
		return EDIT_PARAMS_VALID;
	}
	public void setEDIT_PARAMS_VALID(boolean eDIT_PARAMS_VALID) {
		EDIT_PARAMS_VALID = eDIT_PARAMS_VALID;
	}
	public TimeZone getTimeZone() {
		return timeZone;
	}
	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}
	public List<AssetMaster> getAllAssetsList() {
		return allAssetsList;
	}
	public void setAllAssetsList(List<AssetMaster> allAssetsList) {
		this.allAssetsList = allAssetsList;
	}

	public String getSelectedViewAssetType() {
		return selectedViewAssetType;
	}

	public void setSelectedViewAssetType(String selectedViewAssetType) {
		this.selectedViewAssetType = selectedViewAssetType;
	}

	public String getSelectedViewAssetStatus() {
		return selectedViewAssetStatus;
	}

	public void setSelectedViewAssetStatus(String selectedViewAssetStatus) {
		this.selectedViewAssetStatus = selectedViewAssetStatus;
	}

	public int getViewSno() {
		return viewSno;
	}

	public void setViewSno(int viewSno) {
		this.viewSno = viewSno;
	}

	public Map<String,String> getAssetStatusMap() {
		return assetStatusMap;
	}

	public void setAssetStatusMap(Map<String,String> assetStatusMap) {
		this.assetStatusMap = assetStatusMap;
	}

	public Map<String,String> getBusinessImpactMap() {
		return businessImpactMap;
	}

	public void setBusinessImpactMap(Map<String,String> businessImpactMap) {
		this.businessImpactMap = businessImpactMap;
	}

	public Map<String,String> getAssetCategoryMap() {
		return assetCategoryMap;
	}

	public void setAssetCategoryMap(Map<String,String> assetCategoryMap) {
		this.assetCategoryMap = assetCategoryMap;
	}

	public CustomLookup getCustomLookUp() {
		return customLookUp;
	}

	public void setCustomLookUp(CustomLookup customLookUp) {
		this.customLookUp = customLookUp;
	}

	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}

	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}

}
